package com.xzl.manger.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzl.manger.model.Goods;
import com.xzl.manger.model.GoodsType;
import com.xzl.manger.server.GoodsServer;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("good")
public class GoodsController {
    @Autowired
    private GoodsServer goodsServer;
    @RequestMapping("pagegoods")
    public ModelAndView pagegood(Goods good){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Goods/GoodsPage");
        PageHelper.startPage(good.getPageNum(),good.getPageSize());
        List<Goods> list = goodsServer.selectGoodsOnSale(good);
        PageInfo pageinfo = new PageInfo<>(list);
        mv.addObject("pageinfo",pageinfo);
        mv.addObject("obj",good);
        mv.addObject("typelist",goodsServer.selectTypes());
        return mv;
    }
    @RequestMapping("pagenosale")
    public ModelAndView pagenosale(Goods good){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Goods/GoodsNoSale");
        PageHelper.startPage(good.getPageNum(),good.getPageSize());
        List<Goods> list = goodsServer.selectGoodsNoSale(good);
        PageInfo pageinfo = new PageInfo<>(list);
        mv.addObject("pageinfo",pageinfo);
        mv.addObject("obj",good);
        mv.addObject("typelist",goodsServer.selectTypes());
        return mv;
    }
    @RequestMapping("toinsert")
    public ModelAndView toinsert(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Goods/GoodsInsert");
        mv.addObject("typelist",goodsServer.selectTypes());
        return mv;
    }
    @RequestMapping("insert")
    public void insertGoods(Goods good){
        Integer i = goodsServer.insertGoods(good);
        System.err.println(i);
    }
    @RequestMapping("toupdate")
    public ModelAndView toupdate(Goods good){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Goods/GoodsUpdate");
        List<Goods> list = goodsServer.selectGoods(good);
        mv.addObject("good",list.get(0));
        mv.addObject("typelist",goodsServer.selectTypes());
        return mv;
    }
    @RequestMapping("update")
    public void updateGoods(Goods good){
        Integer i = goodsServer.updateGoods(good);
        System.err.println(i);
    }
    @RequestMapping("upload")
    public JSONObject upload(@RequestParam("file") MultipartFile imgFile)throws IOException{

        InputStream in = null;
        OutputStream out = null;
        String path = "D:/workspace/GoodShop/src/main/resources/static/img/";
        File file = new File(path);
        String name = "";
        try {
        in = imgFile.getInputStream();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        name += simpleDateFormat.format(date) + ".png" ;
        out = new FileOutputStream(new File(path+name));
        IOUtils.copy(in,out);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        finally {
        if(in!=null) in.close();
        if (out!=null) out.close();

        }
        JSONObject json = new JSONObject();
        json.put("imgUrl",name);
        return  json;
    }
    @RequestMapping("down")
    public ModelAndView down(String ids){
        goodsServer.downGoods(ids);

        ModelAndView mv = new ModelAndView();
        mv = pagegood(new Goods());
        return mv;
    }
    @RequestMapping("up")
    public ModelAndView up(String ids){
        goodsServer.upGoods(ids);

        ModelAndView mv = new ModelAndView();
        mv = pagenosale(new Goods());
        return mv;
    }
    @RequestMapping("addtype")
    public void addtype(GoodsType type){
        Integer i = goodsServer.insertTypes(type);
        System.err.println(i);
    }
    @RequestMapping("type")
    public ModelAndView type(){
        ModelAndView mv = new ModelAndView();
        List<GoodsType> list = goodsServer.selectTypes();
        mv.addObject("types",list);
        mv.setViewName("Goods/TypePage");
        return mv;
    }
    @RequestMapping("toaddtype")
    public ModelAndView toaddtype(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Goods/addtype");
        return mv;
    }

}




































